package com.dyna.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dyna.core.utils.PageUtil;
import com.dyna.domain.resp.R;
import com.dyna.enums.CommonStatusEnum;
import com.dyna.foundation.domain.entity.SysDept;
import com.dyna.foundation.domain.form.SysDeptSaveForm;
import com.dyna.foundation.domain.req.SysDeptListReq;
import com.dyna.foundation.domain.resp.SysDeptPageResp;
import com.dyna.foundation.domain.resp.SysDeptResp;
import com.dyna.foundation.domain.resp.SysDeptSimpleResp;
import com.dyna.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author zhoucaiwang
 * @date 2023-08-05
 */
@Api(value = "部门")
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/sys-dept")
public class SysDeptController {

    private final SysDeptService sysDeptService;

    @PostMapping("create")
    @ApiOperation(value = "创建部门")
    public R<Long> createDept(@Valid @RequestBody SysDeptSaveForm createReqVO) {
        Long deptId = sysDeptService.createDept(createReqVO);
        return R.data(deptId);
    }

    @PutMapping("update")
    @ApiOperation(value = "更新部门")
    public R<Boolean> updateDept(@Valid @RequestBody SysDeptSaveForm updateReqVO) {
        sysDeptService.updateDept(updateReqVO);
        return R.data(true);
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "删除部门")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024")})
    public R<Boolean> deleteDept(@RequestParam("id") Long id) {
        sysDeptService.deleteDept(id);
        return R.data(true);
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取部门列表")
    public R<List<SysDeptPageResp>> getDeptList(SysDeptListReq reqVO) {
        List<SysDept> list = sysDeptService.getDeptList(reqVO);
        List<SysDeptPageResp> result = PageUtil.convertPageResult(list, SysDeptPageResp.class);
        return R.data(result);
    }

    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @ApiOperation(value = "获取部门精简信息列表", notes = "只包含被开启的部门，主要用于前端的下拉选项")
    public R<List<SysDeptSimpleResp>> getSimpleDeptList() {
        SysDeptListReq req = new SysDeptListReq();
        req.setStatus(CommonStatusEnum.ENABLE.getStatus());
        List<SysDeptSimpleResp> list = sysDeptService.getSimpleDeptList(req);
        return R.data(list);
    }

    @GetMapping("/get")
    @ApiOperation(value = "获得部门信息")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024")})
    public R<SysDeptResp> getDept(@RequestParam("id") Long id) {
        SysDept dept = sysDeptService.getDept(id);
        return R.data(BeanUtil.toBean(dept, SysDeptResp.class));
    }

}